package com.r_z.service.impl;

import com.r_z.entity.Goods;
import com.r_z.entity.PurchasePlan;
import com.r_z.entity.Repertory;
import com.r_z.mapper.GoodsMapper;
import com.r_z.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2023/6/4
 */
@Service
public class GoodsImpl implements GoodsService {
    @Resource
    GoodsMapper goodsMapper;

    @Override
    public List<PurchasePlan> selectPurchasePlan() {
        return null;
    }

    /**
     * 新增purchasePlan中间表的数据
     * @param purchasePlan
     * @return
     */
    @Override
    public int insertPurchasePlan(PurchasePlan[] purchasePlan) {
        List<PurchasePlan> purchasePlanList = goodsMapper.selectPurchase();
                for (PurchasePlan plan : purchasePlan) {
                    int countPurchase =goodsMapper.selectPurchase2(plan.getGoodId());
                    if (purchasePlanList.size() == 0) {
                        goodsMapper.insertPurchasePlan(plan);
                    } else {
                    for (PurchasePlan purchasePlan1 : purchasePlanList) {
                        if(plan.getGoodId()==purchasePlan1.getGoodId()){
                            goodsMapper.updatePurchasePlan(new PurchasePlan(plan.getGoodId(),
                                    plan.getGoodName(),
                                    plan.getGoodUrl(),
                                    plan.getPrice(),
                                    purchasePlan1.getQuantity()+plan.getQuantity(),plan.getBrandId()));
                            break;
                        }if(countPurchase==0){
                            goodsMapper.insertPurchasePlan(plan);
                            break;
                        }
                    }
                    }
                }

        return purchasePlan.length >= 1 ? 1 : 0;
    }

    /**
     * 查询中间表的条数
     * @param page
     * @param limit
     * @param strWhere
     * @return
     */
    @Override
    public List<PurchasePlan> selectPurchasePlan(int page, int limit, String strWhere) {
        int pages = (page - 1) * limit;
        String where = buildStrWhere(strWhere);
        return goodsMapper.selectPurchasePlan(pages, limit, where);
    }

    /**
     * 查询中间表的条数进行分页
     * @param strWhere
     * @return
     */
    @Override
    public int countPurchasePlan(String strWhere) {
        return goodsMapper.CountPurchasePlan(buildStrWhere(strWhere));
    }

    /**
     * 删除goods中间表
     * @param goodId
     * @return
     */
    @Override
    public int deletePurchasePlan(int goodId) {
        return goodsMapper.deletePurchasePlan(goodId);
    }

    @Override
    public boolean addPurchasePlan(List<PurchasePlan> purchasePlan) {
        return false;
    }

    @Override
    public boolean updatePurchasePlan(PurchasePlan plan) {
        return false;
    }

    @Override
    public List<Goods> LimitselectGoods(int page, int limit, String strShere) {
        int pages = (page - 1) * limit;
        String where = buildStrWhere(strShere);
        return goodsMapper.LimitselectGoods(pages, limit, where);
    }


    @Override
    public int countselectGoods(String where) {
        return goodsMapper.countselectGoods(buildStrWhere(where));
    }



    public String buildStrWhere(String goodName) {
        StringBuffer strWhere = new StringBuffer("");
        if (goodName != null && !goodName.equals("")) {
            strWhere.append(" and goodName like '%" + goodName + "%'");
        }
        return strWhere.toString();
    }
}
